<template>

  <el-dialog width="30%" title="新增员工" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">

        <el-date-picker
          v-model="formData.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用方式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" placeholder="选择聘用方式">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" @focus="showDepts=true" />
        <el-tree
          v-show="showDepts"
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
          @node-click="checkDepts"
        />

      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>

    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="comfirmFn">确定</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-row>
    </template></el-dialog>
</template>

<script>
import employees from '@/api/constant/employees'
import { getDepartments } from '@/api/departments'
import { addEmployees } from '@/api/employees'
import { listToTree } from '@/utils/index'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false }
  },
  data() {
    return {
      showDepts: false,
      hireType: employees.hireType,
      depts: [],
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号码格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请输入入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请输入转正时间', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 获取部门列表
    const res = await getDepartments()
    // console.log(71, res)
    this.depts = listToTree(res.depts, '')
  },
  methods: {
    async comfirmFn() {
      await this.$refs.form.validate()
      await addEmployees(this.formData)
      this.closeFn()
      this.$emit('updateEvent')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.form.resetFields()
    },
    checkDepts(obj) {
      this.formData.departmentName = obj.name
      this.showDepts = false
    }
  }
}
</script>

<style scoped lang="scss">
.el-form-item {
    position: relative;
}
.el-tree {
    position: absolute;
    z-index: 1;
    width: 100%;
    height: 150px;
    overflow: auto;
}
</style>
